
var body = $('body');
var map =  $(".map-dot");
if ($(".map-branch")) {
    // //默认显示上海
    // $(".map-dot.shanghai").addClass("cur");
    // $("#company_shanghai").show().css({
    //     "left": $(".map-dot.shanghai").position().left + 36,
    //     "top": $(".map-dot.shanghai").position().top - $("#company_shanghai").height() / 2 + 8
    // });
    body.on('mouseover', '.map-dot', function () {
        $(this).addClass("cur").siblings(".map-dot").removeClass("cur");
    });
    body.on('click', '.map-dot', function () {
        $(this).addClass("cur").siblings(".map-dot").removeClass("cur");
        var dataid = $(this).attr("data-id")
            , comp = $("#company_" + dataid)
            , otherComp = $("#company_" + dataid).siblings(".branch-map-data");
        comp.fadeIn("fast").css({
            "left": $(this).position().left + 36,
            "top": $(this).position().top - comp.height() / 2 + 8
        });
        otherComp.hide();
    });
    body.on('mouseleave', '.branch-map-data', function () {
        //console.log("离开");
        $(this).hide();
        $(".map-dot").removeClass("cur");
    });

}

function getAreaList(){
    $.ajax({
        type:"get",
        url:"getRecyclingAreaList",
        success:function (data) {
            var Province = [];
            var city = [];
            var neighbourhood = [];
            var addTime = [];
            var ConvertPinYin = [];

            //console.log(data);

            for (var i = 0; i<data.length;i++){
                Province[i] = data[i].province;
                city[i] = data[i].city;
                addTime[i] = data[i].addTime;
                neighbourhood[i] = data[i].neighbourhood;
            }
            //console.log(city);
            Province.sort();
            jQuery.uniqueSort(Province);

            var map_content = $('.map-content-box');
            var branch_map = $('.branch-map-l');
            var map_data = [];
            map_content.empty();
            //console.log(Province);
            for(var j = 0;j<Province.length;j++){
                var str=Province[j].substring(0,Province[j].length-1);
                ConvertPinYin[j] = getPinYinByName(str);
                //console.log(ConvertPinYin[j]);
                map_content.append("<span class='map-dot "+ConvertPinYin[j]+"' data-id="+ ConvertPinYin[j] +" value="+str+">"+str+"</span>");
                map_data[j] = $("<div class='branch-map-data' id='company_"+ConvertPinYin[j]+"'></tr>");
                map_data[j].appendTo(branch_map);
                var title = $("<div class='branch-map-data-title'><p>"+Province[j]+"</p></div>");
                title.appendTo(map_data[j]);
                var info = $("<div class='branch-map-data-info'></div>");
                info.appendTo(map_data[j]);
                for(var k = 0;k<data.length;k++){
                    if(data[k].province===Province[j]){
                        var branch = $("<div class='data-branch'></div>");
                        branch.appendTo(map_data[j]);
                        branch.append("<p class='data-name'>"+ neighbourhood[k] +"</p>");
                        branch.append("<p>地址："+ city[k] +"</p>");
                        branch.append("<p>开通时间："+ addTime[k] +"</p>");
                    }

                }
            }

        }
    })
}

window.onload=function () {
    getAreaList();
};